<?php
	
	class DBAccess
	{
	
		/*
		** Checks to see if a user is in the DB
		*/
		public function IsUserInDB($email)
		{			
			$dblink = $this->DBConnect();
			
			$result = $this->RunSQL("SELECT Email FROM UserIdentity WHERE Email LIKE '" . $email . "'");
		
			$numrecords = count($result);
						
			if($numrecords > 0)
			{
				return true;	
			}else
			{
				return false;	
			}
			
		}
		
		
		
		/*
		** Work out which mode the connection mamanger will opperate in 
		*/
		public function GetMode()
		{
			
			$driverOK = true;
			
			// Check for the sqlsrv driver
			try
			{
				$link = new PDO ("sqlsrv:server=" . DB_HOST . ";database=" . DB_NAME,DB_USERNAME,DB_PASSWORD);	
			}catch(Exception $ex)
			{
				$driverOK = false;
			}
			
			
			if($driverOK)
			{
				return 1;
			}
			else if(function_exists("sqlsrv_connect"))
			{
				return 2;
			}
			else if(function_exists("mssql_connect"))
			{
				return 3;
			}
			else
			{	
				// This mode means no connection can be made to the MSSQL database
				return 0;	
			}
		}
		
		
		/*
		** Execute an SQL query and retun an array of row values
		*/
		public function RunSQL($sql)
		{
			$mode = $this->GetMode() or die("Can't get mode");
			$link = $this->DBConnect() or die(print_r($link->errorInfo(), true));
			
			
			switch($mode)
			{
				case 1:
				
					$i = 0;
					$rows = NULL;
					
					// determin if this query needs a result set
					$chunks = explode(" ", $sql);
					
					if(strtoupper($chunks[0]) == "SELECT")
					{
						// we have a result set form a select statment to we need to use the 
						// preapre function
						$stmt = $link->prepare($sql);
						$stmt->execute();
						
						while($r = $stmt->fetch())
						{
							$rows[$i] = $r;
							$i++;	
						}
						
					}else
					{
						// we are doing something that does not need a result set
						// so we can use the exec function
						
						$count = $link->exec($sql) or die(print_r($link->errorInfo(), true));
						
						$rows = array("rowcount" => $count);
						
					}
					
					$this->DBFree($link);
					
					return $rows;
									
					break;
				
				case 2:
					
					$result = sqlsrv_query($sql);
					
					$rows = NULL;
					$i = 0;
					
					while($r = sqlsrv_fetch_array($result))
					{
						$rows[$i] = $r; 
						$i++;
					}
					
					$this->DBFree($link);
					
					return $rows;
					
					break;
				
				case 3:
				
					$result = mssql_query($sql);
					
					$rows = NULL;
					$i = 0;
					
					while($r = mssql_fetch_array($result))
					{
						$rows[$i] = $r;
						$i++;
					}
					
					$this->DBFree($link);
					
					return $rows;					
					break;
									
				default:
					// no connection can be made
					die("No connection to the database can be made");
				break;	
				
			}
		}
		
		public function DBFree($l)
		{
			switch($this->GetMode())
			{
				case 1:
					unset($l);
					break;
					
				case 2:
					sqlsrv_close($l);
					break;
					
				case 3:	
					mssql_close($l);
					break;
			}
		}
	
		/*
		** Create a connection to the database. This function impliments three differnt ways of 
		** connecting to ensure that atleast one is always suppoerted.
		*/
		public function DBConnect()
		{
			$mode = $this->GetMode();
			
			if($mode == 1)
			{
				    
				$link = new PDO ("sqlsrv:server=" . DB_HOST . ";database=" . DB_NAME,DB_USERNAME,DB_PASSWORD);
				 
				return $link; 

			}
			else if($mode == 2)
			{
				$connectionInfo = array("UID" => DB_USERNAME, "PWD" => DB_PASSWORD, "Database"=>DB_NAME);
				
				$link = sqlsrv_connect("(" . DB_HOST . ")", $connectionInfo);
						
				sqlsrv_select_db(DB_NAME, $link);
			
				return $link;
			}
			else if($mode == 3)
			{
				$link = mssql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
						
				mssql_select_db(DB_NAME, $link);
			
				return $link;
				
			}
			else
			{
				die("No connection to the database can be made");	
			}
		}
	}
	
	
	

?>